package com.hebeu.cglib;

import com.hebeu.proxy.ProducerImpl;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 这是修改的地方
 */
public class Clien {
    public static void main(String[] args) {
        final Producer producer = new Producer();
        Producer cglibProducer= (Producer) Enhancer.create(producer.getClass(), new MethodInterceptor() {
            @Override
            public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
                Float money = (Float) args[0];
                Object retvle = null;
                if (method.getName().equals("saleProduct")) {
                    retvle = method.invoke(producer, money * 0.9f);
                }
                if (method.getName().equals("afterService")) {
                    retvle = method.invoke(producer, money * 0.05f);
                }
                return null;
            }
        });
        cglibProducer.afterService(20000f);
    }
}
